<?php

namespace App\Http\Controllers\admin\comment;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Models\Comment;
use App\Models\Replay;

class CommentController extends Controller
{
    //admin绑定评论数据
    public function index()
    {
        $list = Comment::all('id','level','content','status');

        foreach ($list as $v){
            $v->user = Comment::find($v->id)->user->username;
            $v->product = Comment::find($v->id)->product->name;
            $v->brand = Comment::find($v->id)->product->brand;
            $v->store = Comment::find($v->id)->product->store->name;
            $v->replay = Comment::find($v->id)->replays->count();
            if($v->replay!=0){

                $v->repstatus = Comment::find($v->id)->replays[0]->status;
            }


        }
        //$list = Comment::find(1)->replays[0];
        //dd($list);
        return view('admin.comment.comment',['list'=>$list]);

    }

    public function content(Request $request)
    {
        $id = $request->input('id');

        $data = Comment::find($id,['content']);

        return $data;
    }

    public function replay(Request $request)
    {
        $data = $request->only('comment_id','content');

        $data['user_id'] = 3;

        $replay = Replay::create($data);

        $bool = $replay->save();

        if($bool){
            return 1;
        }else{
            return 0;
        }



    }

    //评论回复的后台修改状态
    public function comStatus(Request $request)
    {
        $id = $request->input('id');

        $data = Comment::find($id);
        $replay = Comment::find($id)->replays->count();
        //评论有 回复也有 锁定评论
        if($data->status == 1 && $replay!=0){
            $data->status = 2;
            $bool = $data->save();
            $repdata = Comment::find($id)->replays[0];
            //回复没有被锁定
            if($repdata->status == 1){
                $repdata->status =2 ;

                $bool = $repdata->save();

                return 1;
            }
            //评论已经被锁定
            return 2;
            //评论没回复 锁定评论
        }else if ($data->status == 1 ){
            $data->status = 2;
            $bool = $data->save();
            return 3;
            //评论有 回复也有 解除锁定
        }else if($data->status == 2 && $replay!=0){
            $data->status = 1;
            $bool = $data->save();
            $repdata = Comment::find($id)->replays[0];
            //回复没有被锁定
            if($repdata->status == 1){

                return 4;
                //回复被锁定
            }else{
                return 5;
            }

        }
        //评论没回复 解除锁定
        else{
            $data->status = 1;
            $bool = $data->save();
            return 6;
        }



    }
    //回复状态锁定和解锁
    public function repStatus(Request $request)
    {
        $id = $request->input('id');

        $data = Comment::find($id)->replays[0];

        $data->status = $data->status==1?2:1;

        $bool = $data->save();

        if ($bool){
            return 1;
        }else{
            return 0;
        }
    }
//    获取回复内容
    public function recontent(Request $request)
    {
        $id = $request->input('id');

        $data = Comment::find($id,['content']);
        $data['recontent'] = Comment::find($id)->replays[0]->content;
        return $data;



    }

}
